<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 1、实现数组的map方法
        Array.prototype.myMap1 = function (fn, thisValue) {
            let res = []
            thisValue = thisValue || []
            let arr = this
            for (let i = 0; i < arr.length; i++) {
                res.push(fn.call(thisValue, arr[i], i, arr))
            }
            return res
        }
        let arr = [1, 2, 3, 4, 5]
        let res1 = arr.myMap1((item, index, arr) => {
            return item * 2
        })
        console.log(res1)
        // 2、reduce实现数组的map方法
        Array.prototype.myMap2 = function (fn, thisValue) {
            let res = []
            thisValue = thisValue || []
            this.reduce(function(pre,cur,index,arr){
                return res.push(fn.call(thisValue,cur,index,arr))
            },[])
            return res
        }
        let res2 = arr.myMap1((item, index, arr) => {
            return item * 3
        })
        console.log(res2)
    </script>
</body>
</html>